#ifndef CODEGENERATOR_H_INCLUDED
#define CODEGENERATOR_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include "List.h"
#include "Parser.h"

#define Var     1
#define Par     2
#define Const   3
#define Fld     4
#define Typ     5
#define Proc    6
#define SProc   7

#define Reg     10
#define Cond    11

#define MOV     0
#define MVN     1
#define ADD     2
#define SUB     3
#define MUL     4
#define Div     5
#define Mod     6
#define CMP     7
#define MOVI    16
#define MVNI    17
#define ADDI    18
#define SUBI    19
#define MULI    20
#define DIVI    21
#define MODI    22
#define CMPI    23
#define CHKI    24
#define LDW     32
#define LDB     33
#define POP     34
#define STW     36
#define STB     37
#define PSH     38
#define RD      40
#define WRD     41
#define WRH     42
#define WRC     43
#define WRL     44
#define BEQ     48
#define BNE     49
#define BLT     50
#define BGE     51
#define BLE     52
#define BGT     53
#define BR      56
#define BL      57
#define HLT     58

#define FP      12
#define SP      13
#define LNK     14
#define PC      15

#define MAXCODE 1000
#define NOOFREG 15

struct Item
{
    int iClass;
    struct Type ptr iType;
    int iAddress;
    int iWrongAddress;
    int iBooleanOperation;
    int iRegister;
};

struct CodeLine
{
    int instruction;
    int reg1;
    int reg2;
    int reg3;
};

int                     programCounter;
struct CodeLine ptr     code[MAXCODE];
int                     registers[NOOFREG];
int                     globalAddress;

function initCodeGenerator();
char ptr getInstruction(int key);
function arithOperation(int operation, struct Item ptr result, struct Item ptr opItem);
function put(int operation, struct Item ptr o1, struct Item ptr o2);
function loadVar(struct Item ptr o);
function freeReg(int reg);
function addCode(int operation, int reg1, int reg2, int reg3);
int getRegister();
function calculateAddresses();
function printItem(struct Item ptr item);


#endif // CODEGENERATOR_H_INCLUDED
